<?php

namespace common\models\DAO;

use Yii;

/**
 * This is the model class for table "mx_product_type".
 *
 * @property integer $id
 * @property string $typeName
 * @property integer $pID
 * @property integer $createTime
 * @property integer $adminID
 */
class ProductType extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'mx_product_type';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parentID', 'createTime', 'adminID'], 'integer'],
            [['typeName'], 'string', 'max' => 50],
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [],
            'add' => ['parentID', 'typeName'],
            'edit' => ['parentID', 'typeName'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'typeName' => '分类名称',
            'parentID' => '父分类',
            'createTime' => '添加时间',
            'adminID' => '添加人',
        ];
    }

    public function beforeSave($insert)
    {
        if($insert){
            $this->createTime = time();
            $this->adminID = \Yii::$app->session->get('ADMIN_ID');;
        }
        return parent::beforeSave($insert); // TODO: Change the autogenerated stub
    }

    public function getAdmin(){
        return $this->hasOne(Admin::className(), ['id' => 'adminID']);
    }
}
